package com.dckeji.shiplock.webapp.conf;

import com.alibaba.druid.filter.Filter;
import com.alibaba.druid.filter.logging.Log4jFilter;
import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import javax.sql.DataSource;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: TianWei
 * Date: 2017/7/28
 * Time: 18:57
 * 数据源配置
 */
@Configuration
public class DataSourceConfig {

    @Value("${jdbc.driver}")
    String jdbcDriver;
    @Value("${jdbc.url}")
    String jdbcUrl;
    @Value("${jdbc.username}")
    String jdbcUserName;
    @Value("${jdbc.password}")
    String jdbcPassword;
    @Value("${druid.filters}")
    String druidFilters;
    @Value("${druid.initialSize}")
    String druidInitialSize;
    @Value("${druid.maxActive}")
    int druidMaxActive;
    @Value("${druid.minIdle}")
    int druidMinIdle;
    @Value("${druid.maxWait}")
    int DruidMaxWait;
    @Value("${druid.removeAbandoned}")
    boolean druidRemoveAbandoned;
    @Value("${druid.removeAbandonedTimeout}")
    int druidRemoveAbandonedTimeout;
    @Value("${druid.timeBetweenEvictionRunsMillis}")
    int druidTimeBetweenEvictionRunsMillis;
    @Value("${druid.minEvictableIdleTimeMillis}")
    int druidMinEvictableIdleTimeMillis;
    @Value("${druid.validationQuery}")
    String druidValidationQuery;
    @Value("${druid.poolPreparedStatements}")
    boolean poolPreparedStatements;
    @Value("${druid.maxPoolPreparedStatementPerConnectionSize}")
    int maxPoolPreparedStatementPerConnectionSize;
    @Autowired
    Log4jFilter log4jFilter;

    @Bean(initMethod = "init", destroyMethod = "close")
    public DataSource druidDataSource() throws SQLException {
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setDriverClassName(jdbcDriver);
        dataSource.setUrl(jdbcUrl);
        dataSource.setUsername(jdbcUserName);
        dataSource.setPassword(jdbcPassword);
        dataSource.setFilters(druidFilters);
        dataSource.setMaxActive(druidMaxActive);
        dataSource.setMinIdle(druidMinIdle);
        dataSource.setMaxWait(DruidMaxWait);
        dataSource.setRemoveAbandoned(druidRemoveAbandoned);
        dataSource.setRemoveAbandonedTimeout(druidRemoveAbandonedTimeout);
        dataSource.setTimeBetweenEvictionRunsMillis(druidTimeBetweenEvictionRunsMillis);
        dataSource.setMinEvictableIdleTimeMillis(druidMinEvictableIdleTimeMillis);
        dataSource.setValidationQuery(druidValidationQuery);
        dataSource.setPoolPreparedStatements(poolPreparedStatements);
        dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolPreparedStatementPerConnectionSize);
        List<Filter> filters = new ArrayList<Filter>();
        filters.add(log4jFilter);
        dataSource.setProxyFilters(filters);
        return dataSource;
    }



}
